library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(ggplot2)
library(readr)
gun_violence <- read_delim("/Users/zzy/Desktop/firearm mortality in each state in 2021 .csv")
## Rows: 50 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): State, abbr
## dbl (3): Year, Death_Rate, Deaths
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
gender <- read_delim("/Users/zzy/Desktop/gender ratio in each state in 2021.csv")
## Rows: 50 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): State, abbr
## dbl (3): Male_Ratio, Female_Ratio, Total_Ratio
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
wealth <- read_delim("/Users/zzy/Desktop/us-real-per-capita-gdp-2022-by-state.csv")
## Rows: 50 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): State, abbr
## num (1): Real_GDP_per_Capita
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
weapon <- read_delim("/Users/zzy/Desktop/number-of-registered-weapons-us-2021-by-state.csv")
## Rows: 50 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): State, abr
## num (1): Number_of_ Registered_Weapon
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

1.The distribution of gun violence in each state in US

library(usmap)

state_geometries <- statepop %>% 
                    filter(full!="District of Columbia")
violence_geometries <- merge(state_geometries, gun_violence, by.x = "full", by.y = "State", all.x = TRUE)

write_csv(violence_geometries, "violence_geometries.csv")

p <- plot_usmap(data = violence_geometries, values = "Death_Rate", color = "red") + 
  scale_fill_continuous(name = "Death Rate", label = scales::comma) + 
  theme(legend.position = "right")+
  ggtitle("The Distribution of Gun Violence in Each State")

p

2.The relationship between gender and gun violence

# merged data
gender_violence <- gun_violence %>% 
                   left_join(gender,by="State")

write_csv(gender_violence, "gender_violence.csv")

gender_violence1 <- gender_violence %>%
           pivot_longer(cols=c(Male_Ratio,Female_Ratio),
                         names_to="gender",
                          values_to="ratio")
gender_violence1 %>% 
  ggplot( aes(x = ratio, y =Death_Rate,color=gender)) +
  geom_point()+
    labs(title = "Relationship between Death Rates and Gender Ratio",
       x = "Gender Ratio",
       y = "Death Rate")

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
gender_violence %>% 
  
  plot_ly(x = ~Death_Rate,
          y = ~Male_Ratio,
          z = ~Female_Ratio,
          text = ~State,
          type = "scatter3d",
           mode = "markers",
          marker = list(size = 5)) %>%
  layout(
    scene = list(
      xaxis = list(title = "Death Rate"),
      yaxis = list(title = "Male Ratio"),
      zaxis = list(title = "Female Ratio")
    ),
    title = "Relationship between Death Rates, Male Ratios, and Female Ratios"
  )

3.The relationship between wealth and gun violence

# merged data

wealth_violence <- gun_violence %>% 
                   left_join(wealth,by="State")

write_csv(wealth_violence, "wealth_violence.csv")

wealth_violence %>% 
  ggplot(aes(x = reorder(State, Real_GDP_per_Capita), y = Death_Rate, color = reorder(State, Real_GDP_per_Capita))) +
  geom_line(aes(group = 1), alpha = 0.7) +
  geom_point(aes(group = 1), size = 3, alpha = 0.7) +
  geom_smooth(method = "lm", se = FALSE, aes(group = 1), color = "yellow3", alpha = 0.5) +  # Adjust alpha here
  labs(title = "Real GDP per Capita by State",
       x = "Real GDP per Capita for states in increasing order",
       y = "Death Rate") +
  theme_minimal() +
  theme(panel.grid = element_blank(),
        axis.text.x = element_blank(),
        axis.line = element_line(color = "#95a5a6"),
        text = element_text(family = "Arial", size = 10))
## `geom_smooth()` using formula = 'y ~ x'

wealth_violence %>% 
  
ggplot( aes(x = reorder(State, Real_GDP_per_Capita), y = Death_Rate, fill = reorder(State, Real_GDP_per_Capita))) +
  geom_bar(stat = "identity", color = "#2c3e50", alpha = 0.7) +
  geom_smooth(method = "le")+
  labs(title = "Real GDP per Capita and the death rate by State in 2021",
       x = "Real_GDP_per_Capita for states in increasing order",
       y = "Death Rate") +
  theme_minimal() +
  theme(panel.grid = element_blank(),
        axis.line = element_line(color = "#95a5a6"),
        text = element_text(family = "Arial", size = 10),
        axis.text.x = element_blank())
## `geom_smooth()` using formula = 'y ~ x'

4.The relationship between number of firearms and gun violence

# merged data

weapon_violence <- gun_violence %>% 
                  left_join(weapon,by="State")


write_csv(weapon_violence, "weapon_violence.csv")

weapon_violence %>% 
  
ggplot( aes(x = reorder(State, Deaths), 
                           y = Deaths, fill = State)) + 
   geom_bar(stat = "identity",show.legend = FALSE) + 
   labs(title = "Number of Deaths in 2021",
        x = "State", y = "Number of Deaths") +
  coord_flip()

weapon_violence %>% 
  
ggplot( aes(x = reorder(State,`Number_of_ Registered_Weapon` ), 
                           y = `Number_of_ Registered_Weapon`, fill = State)) + 
   geom_bar(stat = "identity",show.legend = FALSE) + 
   labs(title = "Number of Registered Weapon in 2021",
        x = "State", y = "Number of Registered Weapon") +
  coord_flip()

weapon_violence %>% 
  ggplot(aes(x = `Number_of_ Registered_Weapon`, y = Deaths, col = State)) +
  geom_point(size = 3, alpha = 0.7) +
  geom_smooth(method = "lm", se = FALSE, col = "skyblue3", fullrange = TRUE,alpha=0.5) +
  labs(title = "Scatterplot of Weapon vs Deaths",
       x = "Number of Registered Weapon",
       y = "Number of Deaths")
## `geom_smooth()` using formula = 'y ~ x'